package com.tbit.main.remote.sms;

import com.alibaba.fastjson.JSON;
import com.tbit.main.util.LogUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 寻程短信
 *
 * @author Leon
 * 2019年3月14日 下午9:04:27
 */
@Slf4j
public class XCSmsClient {
    //APP key
    private static final String APP_KEY = "5f272cb18fac070fbefb93121afc4916";
    //短信模板id
    private static final Integer TPL_ID = 91995;
    // 验证码短信
    private static final String COMMEN_URL = "http://v.apistore.cn/api/v14/xsend";

    /**
     * 发送短信验证码
     *
     * @param phone
     * @param code
     */
    public static void sendAuthCode(String phone, String code) {
        LogUtil.info(phone + "->" + code);

        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", APP_KEY);
        params.put("tpl_id", TPL_ID);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"code\":\"" + code + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
        LogUtil.info(json);
    }

    /**
     * 发送短信验证码
     *
     * @param phone
     * @param code
     */
    public static void sendAuthAccountIdCode(String phone, String code, String appKey, Integer modelId) {
        LogUtil.info(phone + "-->" + code + "-->" + appKey + "-->" + modelId);
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"code\":\"" + code + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
        LogUtil.info("寻程：" + json);
    }

    /**
     * 发送资产预警
     *
     * @param phone
     * @param userCode
     * @param alarm
     * @param appKey
     * @param modelId
     */
    public static void sendMachineAlarm(String phone, String userCode, String alarm, String appKey, String modelId, String accountId) {
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"userCode\":\"" + userCode + "\",\"accountId\":\"" + accountId + "\",\"alarm\":\"" + alarm + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
    }


    /**
     * 发送
     */
    public static void sendSms(String appKey, String modelId, String phone, String paramsJson) {
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", JSON.toJSON(paramsJson));
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
    }

    /**
     * 发送
     */
    public static void sendSms(String appKey, Integer modelId, String phone, String paramsJson) {
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", paramsJson);
        log.info("[XCSmsClient] - [sendSms] [params: {}]", params);
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
        log.info("[XCSmsClient] - [sendSms] [result: {}]", json);
    }

    /**
     * 发送丢失车辆预警
     *
     * @param phone
     * @param userCode
     * @param alarm
     * @param appKey
     * @param modelId
     */
    public static void sendLostMachineAlarm(String phone, String userCode, String alarm, String appKey, String modelId, String accountId, String point) {

        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"userCode\":\"" + userCode + "\",\"accountId\":\"" + accountId + "\",\"alarm\":\"" + alarm + "\",\"point\":\"" + point + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
    }

    /**
     * 发送未还车预警
     *
     * @param phone
     * @param appKey
     * @param modelId
     */
    public static void notReturnCar(String phone, String mins, String appKey, String modelId) {
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"mins\":\"" + mins + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
    }

    /**
     * 发送未支付
     *
     * @param phone
     * @param appKey
     * @param modelId
     */
    public static void notPayment(String phone, String rideTime, String appKey, String modelId) {
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"rideTime\":\"" + rideTime + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
    }

    /**
     * 发送未支付链接
     *
     * @param phone
     * @param appKey
     * @param modelId
     */
    public static void notPaymentLink(String phone, String rideTime, String appKey, String modelId, String linkUrl) {
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"rideTime\":\"" + rideTime + "\",\"linkUrl\":\"" + linkUrl + "\"}");
        String json = APIStore.doPost(COMMEN_URL, APIStore.urlencode(params));
    }

    public static void main(String[] args) {
        //sendAuthCode("13435792203","8888");
        //sendAuthAccountIdCode("15238785583","123456","5f272cb18fac070fbefb93121afc4916",91995);
        //sendMachineAlarm("15238785583","test",null,"b55962a387d4267a0c95168525ae58e0","92153","泰比特");
        //sendMachineAlarmt("15238785583","123","超区","b55962a387d4267a0c95168525ae58e0","92153","yun");
        //notReturnCar("15238785583","10","b55962a387d4267a0c95168525ae58e0","92154");
        //notPayment("15238785583","2020-02-01 02:01:23","b55962a387d4267a0c95168525ae58e0","92155");
        notPaymentLink("15238785583", "10",
                "b55962a387d4267a0c95168525ae58e0", "92579", "weixin://dl/business/?t=zKOXDz7S9Je");
    }

}
